package com.zuoye.nio;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Zuoye01 {
    public static void main(String[] args) throws IOException {
         zuoye1();
        //s666();
       //ssss();

        Path path = Paths.get("E:\\ZuoYe\\新建文件夹");
        Files.walkFileTree(path, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("dir = " + dir);
                boolean exists = Files.exists(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\a.txt"));
                if (exists == true){
                    boolean file = Files.deleteIfExists(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\a.txt"));
                    boolean file1= Files.deleteIfExists(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\b.txt"));
                    boolean file2 = Files.deleteIfExists(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c\\c.txt"));

                    Files.createFile(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\a.txt"));
                    Files.createFile(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\b.txt"));
                    Files.createFile(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c\\c.txt"));



                }else {
                    Files.createFile(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\a.txt"));
                    Files.createFile(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\b.txt"));
                    Files.createFile(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c\\c.txt"));
                }


                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("file = " + file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                System.out.println("file = " + file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("dir = " + dir);
                BufferedWriter writer1 = Files.newBufferedWriter(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\a.txt"));
                writer1.write("abc");
                writer1.close();
                BufferedWriter writer2 = Files.newBufferedWriter(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\b.txt"));
                writer2.write("abc");
                writer2.close();
                BufferedWriter writer3 = Files.newBufferedWriter(Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c\\c.txt"));
                writer3.write("abc");
                writer3.close();
                return FileVisitResult.CONTINUE;
            }
        });


    }

//    private static void ssss() throws IOException {
//        Path path = Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c");
//        Stream<Path> list = Files.list(path);
//        Iterator<Path> iterator = list.iterator();
//        while (iterator.hasNext()){
//            Path next = iterator.next();
//            System.out.println("next = " + next);
//            Path path1 = Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c\\c.txt");
//            Files.createFile(path1);
//            BufferedWriter writer1 = Files.newBufferedWriter(path1);
//            writer1.write("abc");
//
//
//        }
//    }


    private static void zuoye1() throws IOException {
        Path path = Paths.get("E:\\ZuoYe\\新建文件夹\\a\\b\\c");
        Files.createDirectories(path);
    }
}